package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.GoodsCreateBo;
import com.cskaoyan.market.bo.MarketGoodsUpdateBo;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsAttribute;
import com.cskaoyan.market.db.domain.MarketGoodsProduct;
import com.cskaoyan.market.db.domain.MarketGoodsSpecification;
import com.cskaoyan.market.service.AdminGoodsService;
import com.cskaoyan.market.service.impl.AdminGoodsServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MarketConstant;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:yanzijun
 * @time:2024/06/18/14:33
 */
@WebServlet("/admin/goods/*")
public class AdminGoodsController extends HttpServlet {
    AdminGoodsService adminGoodsService = new AdminGoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replaced = requestURI.replace(req.getContextPath() + "/admin/goods/", "");
        if ("list".equals(replaced)) {
            list(req, resp);
        } else if ("detail".equals(replaced)) {
            detail(req, resp);
        } else if ("catAndBrand".equals(replaced)) {
            catAndBrand(req, resp);
        }
    }

    private void catAndBrand(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, Object> map = adminGoodsService.catAndBrand();
        Object ok = ResponseUtil.ok(map);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        HashMap<String, Object> map = new HashMap<>();
        int goodId = Integer.parseInt(id);
        MarketGoods marketGood = adminGoodsService.getMarketgood(goodId);
        List<MarketGoodsAttribute> marketGoodsAttributeList = adminGoodsService.getMarketgoodAttributes(goodId);
        List<MarketGoodsProduct> marketGoodsProductList = adminGoodsService.getProducts(goodId);
        List<MarketGoodsSpecification> marketGoodsSpecificationList = adminGoodsService.getSpecification(goodId);
        int[] array = adminGoodsService.getCategoryIds(id);
        map.put("categoryIds", array);
        map.put("goods", marketGood);
        map.put("attributes", marketGoodsAttributeList);
        map.put("products", marketGoodsProductList);
        map.put("specifications", marketGoodsSpecificationList);
        Object ok = ResponseUtil.ok(map);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String id = req.getParameter("goodsId");
        String goodsSn = req.getParameter("goodsSn");
        String name = req.getParameter("name");
        List<MarketGoods> marketGoodsList = adminGoodsService.list(page, limit, sort, order, id, goodsSn, name);
        Object o = ResponseUtil.okList(marketGoodsList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replaced = requestURI.replace(req.getContextPath() + "/admin/goods/", "");
        if ("update".equals(replaced)) {
            update(req, resp);
        } else if ("delete".equals(replaced)) {
            delete(req, resp);
        } else if ("create".equals(replaced)) {
            create(req, resp);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String requestBody = reader.readLine();
        GoodsCreateBo goodsCreateBo = JacksonUtil.getObjectMapper().readValue(requestBody, GoodsCreateBo.class);
        adminGoodsService.create(goodsCreateBo.getGoods(), goodsCreateBo.getSpecifications(), goodsCreateBo.getAttributes(), goodsCreateBo.getProducts());

        //抓取公网上面的返回结果
        Object ok = ResponseUtil.ok();
        String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
        resp.getWriter().println(s);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String readLine = req.getReader().readLine();
        MarketGoods marketGoods = JacksonUtil.getObjectMapper().readValue(readLine, MarketGoods.class);
        //num=1 说明删除成功
        //num!=1 说明删除失败
        Integer num = adminGoodsService.delete(marketGoods);
        if (num == 1) {
            Object ok = ResponseUtil.ok();
            String s = JacksonUtil.getObjectMapper().writeValueAsString(ok);
            resp.getWriter().println(s);
        } else {
            Object o = ResponseUtil.fail(MarketConstant.WRONG_DELETE, "删除失败");
            String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String readLine = req.getReader().readLine();
        MarketGoodsUpdateBo marketGoodsUpdateBo = JacksonUtil.getObjectMapper().readValue(readLine, MarketGoodsUpdateBo.class);
        int num = adminGoodsService.update(marketGoodsUpdateBo.getGoods(), marketGoodsUpdateBo.getAttributes(), marketGoodsUpdateBo.getProducts(), marketGoodsUpdateBo.getSpecifications());


        //num=1 说明更新成功
        //num=2 说明商品编号为空
        //num=3 说明商品名字为空
        Object o = null;
        if (num == 1) {
            o = ResponseUtil.ok();

        } else if (num == 2) {
            o = ResponseUtil.fail(MarketConstant.WRONG_GOODSN, "商品编号不能为空");

        } else if (num == 3) {
            o = ResponseUtil.fail(MarketConstant.WRONG_GOODSNAME, "商品名字不能为空");

        }
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);

    }
}
